package com.graduation.springbootinit.controller;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.model.entity.ShopPart.ShoppingCartItem;
import com.graduation.springbootinit.model.vo.ShoppingCartItemVO;
import com.graduation.springbootinit.model.vo.StoreCartGroupVO;
import com.graduation.springbootinit.service.ShoppingCartService;
import com.graduation.springbootinit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api/shopPart/cart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService cartService;



    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addToCart(
            @RequestBody ShoppingCartItem cartItem,
            HttpServletRequest request) {
        Long  userId = userService.getLoginUser(request).getId();
        return cartService.addToCart(userId, cartItem);
    }


    @PostMapping("/update/quantity")
    public BaseResponse<Boolean> updateQuantity(
            @RequestBody ShoppingCartItem cartItem,
            HttpServletRequest request) {
        Long  userId = userService.getLoginUser(request).getId();
        return cartService.updateQuantity(userId, cartItem);
    }


    @PostMapping("/remove")
    public BaseResponse<Boolean> removeFromCart(
            @RequestBody ShoppingCartItem cartItem,
            HttpServletRequest request) {
        Long  userId = userService.getLoginUser(request).getId();
        return cartService.removeFromCart(userId, cartItem);
    }


    @GetMapping("/list")
    public BaseResponse<List<ShoppingCartItemVO>> getCartItems(HttpServletRequest request) {
        Long  userId = userService.getLoginUser(request).getId();
        return cartService.listUserCartItems(userId);
    }


    @PostMapping("/update/selected")
    public BaseResponse<Boolean> updateSelected(
            @RequestBody ShoppingCartItem cartItem,
            HttpServletRequest request) {
        Long  userId = userService.getLoginUser(request).getId();
        return cartService.updateSelected(userId, cartItem);
    }

    @ApiOperation("获取按商户分组的购物车商品列表")
    @GetMapping("/list/byStore")
    public BaseResponse<List<StoreCartGroupVO>> getCartItemsByStore(HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        return cartService.listUserCartItemsByStore(userId);
    }
} 